layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage','laytpl','util'], function () {
    var form = layui.form
        , layer = parent.layer === undefined ? layui.layer : parent.layer
        , laypage = layui.laypage,laytpl = layui.laytpl, $ = layui.jquery;

    dataList(1);

    /**
     * 查询
     */
    form.on('submit(queryForm)', function(data){
        $("#number").val(1);
        dataList(1);
        return false;
    });

    /**
     * 查询数据列表
     * @param number
     */
    function dataList(number) {
        var options = {
            dataType: "json",
            success: function (data) {
                if(data && data.error) {
                    layer.msg(data.error.msg);
                    return;
                }
                var listData = {"list":data.page.records};
                var getTpl = dataListTpl.innerHTML,view = document.getElementById('listDataBody');

                laytpl(getTpl).render(listData, function(html){
                    view.innerHTML = html;
                });
                form.render();
                if(number === 1) {
                    //分页标签
                    pageBar(data.page.total,data.page.size);
                }
            },
            error: function (e) {
                layer.msg(JSON.stringify(e));
            }
        };
        $("#listForm").ajaxSubmit(options);
    }

    /**
     * 数据分页
     * @param count
     * @param limit
     */
    function pageBar(count,limit) {
        laypage.render({
            elem: "page",
            limit: limit,
            count: count,
            theme: '#1E9FFF',
            layout: ['count', 'prev', 'page', 'next'],
            jump: function(obj, first){
                if(!first){
                    $("#number").val(obj.curr);
                    dataList(obj.curr);
                }
            }
        });
    }


    /**
     * 审核博客
     */
    form.on('switch(blogTypeFrozen)', function (obj) {
        var id = obj.elem.getAttribute("data-id");
        var type = 1;
        if (obj.elem.checked) {
            type = 0;
        }
        obj.elem.disabled = true;
        post("/m/blog/type/" + id, {
            "type": type
        }, function (data) {
            if (data.code == 0) {
                obj.elem.disabled = false;
                layer.msg(data.msg, {icon: 6, time: 500});
            }
        });
    })

    /**
     * 推荐博客
     */
    form.on('switch(blogRecommendTypeFrozen)', function (obj) {
        var id = obj.elem.getAttribute("data-id");
        var recommendType = 1;
        if (obj.elem.checked) {
            recommendType = 0;
        }
        obj.elem.disabled = true;
        post("/m/blog/recommend/" + id, {
            "recommendType": recommendType
        }, function (data) {
            if (data.code == 0) {
                obj.elem.disabled = false;
                layer.msg(data.msg, {icon: 6, time: 500});
            }
        });
    })

    //全选
    form.on('checkbox(allChoose)', function(data){
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
        child.each(function(index, item){
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });

    //通过判断文章是否全部选中来确定全选按钮是否选中
    form.on("checkbox(choose)",function(data){
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
        var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
        if(childChecked.length == child.length){
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
        }else{
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
        }
        form.render('checkbox');
    })
})
